<template>
  <el-container>
    <el-header>
      <div class="picbox">
        <img src="../assets/logo.png" alt />
        <span>vue后台管理</span>
      </div>
      <el-button type="info" @click="logout">溜了</el-button>
    </el-header>

    <el-container>
      <el-aside :width=" isActive ? '64px':'200px'">
        <div class="collapse" @click="collapse">|||</div>
        <el-menu
          :default-active="_routerRoot._route.fullPath"
          class="el-menu-vertical-demo"
          background-color="pink"
          text-color="#fff"
          active-text-color="purple"
          :unique-opened="true"
          :collapse-transition="false"
          :collapse="isActive"
          router
        >
          <el-submenu :index="item.id + ''" v-for="item in menuLists" :key="item.id">
            <!-- 一级菜单 -->
            <template slot="title">
              <i :class="iconObj[item.order]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index=" '/'+ infoitem.path"
              v-for="infoitem in item.children"
              :key="infoitem.id"
              @click="setPath('/' + infoitem.path)"
            >
              <i class="el-icon-s-help"></i>
              {{infoitem.authName}}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      path: '',
      isActive: false,
      menuLists: [],
      iconObj: {
        1: 'el-icon-user-solid',
        2: 'el-icon-lock',
        3: 'el-icon-s-goods',
        4: 'el-icon-document-copy',
        5: 'el-icon-s-data'
      }
    }
  },
  methods: {
    logout() {
      // 清空 sessionStorage 内的值
      window.sessionStorage.clear()
      // 回到登录页面
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuLists = res.data
    },
    collapse() {
      this.isActive = !this.isActive
    },
    // 储存路径
    setPath(path) {
      window.sessionStorage.setItem('path', path)
    }
  },
  created() {
    this.getMenuList()
    this.path = window.sessionStorage.getItem('path')
  }
}
</script>

<style lang="less" scoped>
.is-vertical {
  height: 100%;
}
.el-header {
  background-color: #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  .picbox {
    display: flex;
    align-items: center;
    img {
      height: 60px;
      margin: 0 20px;
    }
  }
}
.el-aside {
  background-color: pink;
  .collapse {
    line-height: 24px;
    background-color: skyblue;
    text-align: center;
    font-size: 14px;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}

.el-main {
  background-color: #fff;
}
.el-menu {
  border-right: none;
}
</style>
